/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.system.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.entity.Role;


public class RoleContext {
	public static Object lock = new Object();
	
	private static Map<String,Role> rolesByid;
	private static List<Role> allRoles;
	private static List<Role> activeRoles;
	private static List<Role> unactiveRoles;
	public static boolean UPDATE_FLAG = true;	//更新标识
	
	public static boolean isUpdate(){
		return UPDATE_FLAG;
	}

	public static void put(Role role){
		if(role!=null){
			if(rolesByid==null){
				rolesByid= new Hashtable<String,Role>();
			}
			if(allRoles==null){
				allRoles=new ArrayList<Role>();
			}
			if(activeRoles==null){
				activeRoles=new ArrayList<Role>();
			}
			if(unactiveRoles==null){
				unactiveRoles=new ArrayList<Role>();
			}
			allRoles.add(role);
			if(role.getActiveStatus()!=null){
				if(role.getActiveStatus()){
					activeRoles.add(role);
				}else{
					unactiveRoles.add(role);
				}
			}
			if(rolesByid.get(role.getRoleId())==null){
				rolesByid.put(role.getRoleId(), role);
			}
		}
	}
	
	public static Role getRoleById(String roleId){
		return rolesByid.get(roleId);
	}
	
	public static List<Role> getAllRoles(Boolean active){
		if(active==null){
			return allRoles;
		}else if(true==active){
			return activeRoles;
		}else{
			return unactiveRoles;
		}
		
	}
	
	public static void remove(){
		if(rolesByid!=null){
			rolesByid.clear();
		}
		if(allRoles!=null){
			allRoles.clear();
		}
		if(activeRoles!=null){
			activeRoles.clear();
		}
		if(unactiveRoles!=null){
			unactiveRoles.clear();
		}
	}
}
